using NodeCanvas.Variables;
using UnityEngine;
using UnityEngine.AI;

namespace NodeCanvas.Actions
{
	[Category("Movement")]
	[AgentType(typeof(NavMeshAgent))]
	public class MoveToGameObject : ActionTask
	{
		[RequiredField]
		public BBGameObject target;

		public BBFloat speed = new BBFloat
		{
			value = 3f
		};

		public float keepDistance = 0.1f;

		private Vector3 lastRequest;

		private NavMeshAgent navAgent
		{
			get
			{
				return (NavMeshAgent)base.agent;
			}
		}

		protected override string info
		{
			get
			{
				return "GoTo " + target.ToString();
			}
		}

		protected override void OnExecute()
		{
			navAgent.speed = speed.value;
			if ((navAgent.transform.position - target.value.transform.position).magnitude < navAgent.stoppingDistance + keepDistance)
			{
				EndAction(true);
			}
			else
			{
				Go();
			}
		}

		protected override void OnUpdate()
		{
			Go();
		}

		private void Go()
		{
			Vector3 position = target.value.transform.position;
			if (lastRequest != position && !navAgent.SetDestination(position))
			{
				EndAction(false);
				return;
			}
			lastRequest = position;
			if (!navAgent.pathPending && navAgent.remainingDistance <= navAgent.stoppingDistance + keepDistance)
			{
				EndAction(true);
			}
		}

		protected override void OnStop()
		{
			lastRequest = Vector3.zero;
			if (navAgent.gameObject.activeSelf)
			{
				navAgent.ResetPath();
			}
		}

		protected override void OnPause()
		{
			OnStop();
		}
	}
}
